﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Logistics.DataManage.DAL;

namespace Logistics.DataManage
{
    class ChargesMgr
    {
        /// <summary>
        /// 根据对应的费率得到折扣后的金额
        /// </summary>
        /// <param name="_Money">要计算的佣金</param>
        /// <param name="_RouteID">路线的ID</param>
        /// <param name="_tag">路线中对应的费率的标示（运费=1,保价费=2,（收）代收款=3, （发）代收款=4, 预收款费率=5, 仓库费费率=6 ,延迟赔付费率=7, 送货费率=8, 转账登记率=9, 快慢件延时赔付率=10 ）</param>
        /// <returns></returns>
        public static float CalcInsuranceFee(float _Money, string _RouteID)
        {
            ORoute route = RouteDAO.GetRouteById(_RouteID);
            string _RatesID = route.InsuredFreeRate;//路线表中对应费率标示字段名
            return GetChargeByRateId(_RatesID, _Money);
        }

        /// <summary>
        /// 计算送货费率
        /// </summary>
        /// <param name="_Money"></param>
        /// <param name="_RouteID"></param>
        /// <returns></returns>
        public static float CalcDeliveryFee(float _Money, string _RouteID)
        {
            ORoute route = RouteDAO.GetRouteById(_RouteID);
            string _RatesID = route.DeliveryFee;//路线表中对应费率标示字段名
            return GetChargeByRateId(_RatesID, _Money);
        }
        public static float CalcAgencyFundHandlingCharge(float _Money, string _RouteID)
        {
            ORoute route = RouteDAO.GetRouteById(_RouteID);
            string _RatesID = route.StartCollectionRate;//路线表中对应费率标示字段名
            return GetChargeByRateId(_RatesID, _Money);
        }

        /// <summary>
        /// 计算运费
        /// </summary>
        /// <param name="siteId_Start">起始站</param>
        /// <param name="siteId_End">目的站</param>
        /// <param name="weight">重量(kg)</param>
        /// <param name="volume">体积(立方米)</param>
        /// <returns>重量计算出的运费和体积计算出的运费值中的大值</returns>
        public static double GetFreightCharge(string siteId_Start, string siteId_End, float weight, float volume)
        {
            double rst_Charge = 0;
            double Volume_Charge = 0;
            double Weighing_Charge = 0;
            ORoute Route = RouteDAO.GetRouteByStartEndId(siteId_Start, siteId_End);
            if (weight > 0)
                Weighing_Charge = GetChargeByRateId_xxxx(Route.WeightRates, weight);
            if (volume > 0)
                Volume_Charge = GetChargeByRateId_xxxx(Route.LiangFangRate, volume);
            //取重量运费和体积运费中的大值
            rst_Charge = Math.Max(Volume_Charge, Weighing_Charge);
            rst_Charge = Math.Ceiling(rst_Charge);
            return rst_Charge;
        }

        /// <summary>
        /// 获取某费率区间对应的费用值
        /// </summary>
        /// <param name="rateInforId">费率明细编号</param>
        /// <param name="enter_Value">输入值</param>
        /// <returns></returns>
        private static float GetChargeByRateId(string rateInforId, float enter_Value)
        {
            float rst_Charge = 0;
            ORatesDetail detail = RatesDetailDAO.GetDetailIdByRateIdAndTagVal(rateInforId, enter_Value);
            if (detail != null)
            {
                //if (detail.Rates == 0 && detail.Amount > 0)//只按固定值计算
                //if (detail.RatesInforMode == 1)
                if (detail.State == 1)
                {
                    rst_Charge = detail.Amount;
                }
                //else if (detail.RatesInforMode == 2)//只按比率计算
                //else if (detail.Rates > 0 && detail.Amount == 0)//只按比率计算
                else if (detail.State == 2)//只按比率计算
                {
                    rst_Charge = detail.Rates * enter_Value;
                    //rst_Charge = Math.Max(rst_Charge, detail.MinLimit);//区段最小值
                    //rst_Charge = Math.Min(rst_Charge, detail.MaxLimit);//区段最大值
                }
//                else if (detail.RatesInforMode == 3)//按比例和固定值计算后取大值
                //else if (detail.Rates > 0 && detail.Amount > 0)//两者都不为空则计算后取大值
                else if (detail.State == 3)//按比例和固定值计算后取大值
                {
                    rst_Charge = detail.Rates * enter_Value;
                    //rst_Charge = Math.Max(rst_Charge, detail.MinLimit);//区段最小值
                    //rst_Charge = Math.Min(rst_Charge, detail.MaxLimit);//区段最大值

                    rst_Charge = Math.Max(detail.Amount, rst_Charge);
                }
            }
            return rst_Charge;
        }

        /// <summary>
        /// 获取梁方称重费率区间对应的费用值
        /// </summary>
        /// <param name="rateInforId">费率明细编号</param>
        /// <param name="enter_Value">输入值</param>
        /// <returns></returns>
        private static float GetChargeByRateId_xxxx(string rateInforId, float enter_Value)
        {
            float rst_Charge = 0;
            ORatesDetail detail = RatesDetailDAO.GetDetailIdByRateIdAndTagVal(rateInforId, enter_Value);
            if (detail != null)
            {
                rst_Charge = detail.Amount * enter_Value;
            }
            return rst_Charge;
        }

        #region 计算源站点收取费用与目的站点收取费用

        /**
         * 计算算法：
         * 1.总计算公式
         * 1)始发站收=始发站收①+始发站收②
         * 2)目的站收=目的站收①+目的站收②
         * 
         * 2.始发站收①，目的站收①（保价费，送货费）
         *  始发站收①=保价费，送货费中 指定收取的始发站收费用和
         *  目的站收①=保价费，送货费中 指定收取的目的站收费用和
         *  
         * 3.始发站收②，目的站收②（按提付方式计算）
         *  1)提付方式
         *      始发站收②=0
         *      目的站收②=运费+代收款+代收款手续费(0)+垫付+转寄费
         *  2)现付方式(或其他)
         *      始发站收②=运费+转寄费
         *      目的站收②=代收款+代收款手续费(0)+垫付
         * */
        #endregion
    }
}
